/*
 *  filecopy.c
 *  Created on: 2012-12-27
 *  Author: Fan
 */
#include "string.h"
#include "stdio.h"
#include <sys/types.h>
#include <dirent.h>
#include "filecopy.h"
#include "USER_CFG.h"



extern _enum_DMODE G_DispMode;
extern volatile unsigned int LIFT_FLAG;
IMGNODE overlay_fbdisplay;

filestruct File_RAM[FOLDER_NUM][FILE_NUM_IN_FOLDER];
unsigned int PIC_RAM_BASE = 0x515C0;
unsigned int pic_addr = 0x515C0;
//unsigned int PIC_RAM_BASE = 0x0;
//unsigned int pic_addr = 0x100;
char bmp_flr[4*1024*1024];
char bmp_flr_data[4*1024*1024];

int filenummax[10];
IMGNODE ptr;

char * SpiFlash_0C[] =
{
//ËÄŒ¶ÎÄŒþŒÐ
	"/mnt/nand1-2/LIFT/LIFT_0C/BKGND/FLR_BSD",
	"/mnt/nand1-2/LIFT/LIFT_0C/SYS/FLR", // Â¥²ãÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_0C/SYS/DIR", // ŒýÍ·ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_0C/SYS/LFTSTE", // µçÌÝ×ŽÌ¬ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_0C/SYS/DATETME", // Ê±ŒäÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_0C/SYS/DAY",     // Ê±ŒäÐÅÏ¢--ÄêÔÂÈÕ
	"/mnt/nand1-2/LIFT/LIFT_0C/SYS/KEY",     // Ä¿µÄ²ãËØ²Ä

	//"/mnt/nand1-2/LIFT/LIFT_0C/SYS/AD",     // ×ÖÄ»ËØ²Ä
};

char * SpiFlash_90C[] =
{
//ËÄŒ¶ÎÄŒþŒÐ
	"/mnt/nand1-2/LIFT/LIFT_90C/BKGND/FLR_BSD",
	"/mnt/nand1-2/LIFT/LIFT_90C/SYS/FLR", // Â¥²ãÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_90C/SYS/DIR", // ŒýÍ·ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_90C/SYS/LFTSTE", // µçÌÝ×ŽÌ¬ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_90C/SYS/DATETME", // Ê±ŒäÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_90C/SYS/DAY",     // Ê±ŒäÐÅÏ¢--ÄêÔÂÈÕ
	"/mnt/nand1-2/LIFT/LIFT_90C/SYS/KEY",    // Ä¿µÄ²ãËØ²Ä

	//"/mnt/nand1-2//LIFT/LIFT_90C/SYS/AD",     // ×ÖÄ»ËØ²Ä
};

char * SpiFlash_180C[] =
{
//ËÄŒ¶ÎÄŒþŒÐ
	"/mnt/nand1-2/LIFT/LIFT_180C/BKGND/FLR_BSD",
	"/mnt/nand1-2/LIFT/LIFT_180C/SYS/FLR", // Â¥²ãÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_180C/SYS/DIR", // ŒýÍ·ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_180C/SYS/LFTSTE", // µçÌÝ×ŽÌ¬ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_180C/SYS/DATETME", // Ê±ŒäÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_180C/SYS/DAY",     // Ê±ŒäÐÅÏ¢--ÄêÔÂÈÕ
	"/mnt/nand1-2/LIFT/LIFT_180C/SYS/KEY",     // Ä¿µÄ²ãËØ²Ä

	//"/mnt/nand1-2/LIFT/LIFT_180C/SYS/AD",     // ×ÖÄ»ËØ²Ä
};

char * SpiFlash_270C[] =
{
//ËÄŒ¶ÎÄŒþŒÐ
	"/mnt/nand1-2/LIFT/LIFT_270C/BKGND/FLR_BSD",
	"/mnt/nand1-2/LIFT/LIFT_270C/SYS/FLR", // Â¥²ãÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_270C/SYS/DIR", // ŒýÍ·ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_270C/SYS/LFTSTE", // µçÌÝ×ŽÌ¬ÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_270C/SYS/DATETME", // Ê±ŒäÐÅÏ¢
	"/mnt/nand1-2/LIFT/LIFT_270C/SYS/DAY",     // Ê±ŒäÐÅÏ¢--ÄêÔÂÈÕ
	"/mnt/nand1-2/LIFT/LIFT_270C/SYS/KEY",     // Ä¿µÄ²ãËØ²Ä

	//"/mnt/nand1-2/LIFT/LIFT_270C/SYS/AD",     // ×ÖÄ»ËØ²Ä
};

char ** pDir_SpiFlash[4] =
{
	SpiFlash_0C,
	SpiFlash_90C,
	SpiFlash_180C,
	SpiFlash_270C,
};

void ChangeNametoLow(char * filename)
{
	while((*filename) != '\0')
	{
		if(((*filename) >= 'A') && ((*filename) <= 'Z'))
		{
			*filename = (*filename) + ('a' - 'A');
		}

		filename++;
	}
}

void InitialFileRAMSruct(void)
{
	printf("InitialFileRAMSruct start...\n");
	unsigned int i;
	unsigned int folder,file;
	pic_addr = 0x515C0;
	for(folder=0;folder<FOLDER_NUM;folder++)
	{

		for(file=0;file<FILE_NUM_IN_FOLDER;file++)
		{
			for(i=0;i<LENGTH_NAME;i++)
			{
				File_RAM[folder][file].name[i] = '\0';
			}
			File_RAM[folder][file].pic_info.img_base = PIC_RAM_BASE;
			File_RAM[folder][file].pic_info.img_xlen = 0;
			File_RAM[folder][file].pic_info.img_ylen = 0;
			File_RAM[folder][file].pic_info.img_bpp = 32;

		}
	}
}

unsigned char CopyFolderToRAM(char* scan_path,unsigned int savepath)
{
	DIR	*dir = NULL;
	FILE	*pic_ptr;
	struct dirent *ptr_f = NULL;
	int i = 0;
	int read_len;
	char    *fn;
	char    *fnhouzhui;
	char temppath[64];
	
	printf("CopyFolderToRAM start...\n");

	dir = opendir(scan_path);                             // Žò¿ªÖž¶šµÄÂ·Ÿ¶
	if (dir == NULL)
		return 1;
	else
	{	
		int file_num=0;
		while(1)
		{
		ptr_f = readdir(dir);
		if (ptr_f ==NULL || ptr_f->d_name == 0) break;
		
            	fn = ptr_f->d_name;

	        if (ptr_f->d_type !=  DT_DIR)                         // ÅÐ¶ÏËÑË÷µœµÄÊÇÎÄŒþ»¹ÊÇÎÄŒþŒÐ
	        {
	    	  fnhouzhui = strchr(fn,'.');                      // ÈôÎªÎÄŒþ£¬¶šÎ»ºó×ºÃû
	    	  fnhouzhui++;

	    	  /*char *strchr(char* _Str,int _Ch)   ²éÕÒ×Ö·ûŽ®sÖÐÊ×ŽÎ³öÏÖ×Ö·ûcµÄÎ»ÖÃ,·µ»ØÊ×ŽÎ³öÏÖcµÄÎ»ÖÃµÄÖžÕë£¬Èç¹ûsÖÐ²»ŽæÔÚcÔò·µ»ØNULL*/
	    	  /*char *strstr(char *str1, char *str2)   ÕÒ³östr2×Ö·ûŽ®ÔÚstr1×Ö·ûŽ®ÖÐµÚÒ»ŽÎ³öÏÖµÄÎ»ÖÃ,·µ»ØžÃÎ»ÖÃµÄÖžÕë£¬ÈçÕÒ²»µœ£¬·µ»Ø¿ÕÖžÕë*/

	    	  if((!strcmp(fnhouzhui,"bmp")) || (!strcmp(fnhouzhui,"BMP")))              // ÅÐ¶Ïºó×ºÊÇ·ñÏàÍ¬
	    	  {

	    		  sprintf(temppath,scan_path);
	    		  strcat(temppath,"/");
	    		  strcat(temppath,fn);
	    		  pic_ptr = fopen (temppath, "r");

	    		  if(pic_ptr>0)
	    		  {
				  read_len = 0;
	    			  read_len = fread(bmp_flr,1,4*1024*1024,pic_ptr);
	    			  if(read_len != 0)
	    			  {
						if((savepath!=0)||(file_num < 0))
						{
					  	BMPINFO* info_flr = bmp_getinfo(bmp_flr);
					  	
					  	if(info_flr->bpp==16)
					  	{
					  		int m=0;
					  		if(info_flr->width*2%4!=0)
								m=4-info_flr->width*2%4;
					  		for(i = 0;i<(int)(info_flr->height);i++)
					  		{
						  		if(info_flr->order==1)//height>0 自下向上扫描 需要逆序后再显示
						  		{			  	
									memcpy(&bmp_flr_data[((int)(info_flr->height)-1-i)*info_flr->bpp/8*(int)(info_flr->width)],\
									info_flr->pixel_array+i*((int)(info_flr->width)*info_flr->bpp/8+m),(int)(info_flr->width)*info_flr->bpp/8);	
								}
								else
								{
									memcpy(&bmp_flr_data[i*info_flr->bpp/8*(int)(info_flr->width)],\
										info_flr->pixel_array+i*((int)(info_flr->width)*info_flr->bpp/8+m),(int)(info_flr->width)*info_flr->bpp/8);	
								}	
							}
								
							unsigned short * pu16Temp;
							unsigned char u8R, u8G, u8B;	
							
							pu16Temp = (unsigned short *) bmp_flr_data;
							memset(bmp_flr,0,4*1024*1024);
							for ( i = 0; i < (int)(info_flr->width)*(int)(info_flr->height); i++ )
							{
								u8R = ((pu16Temp[i] & 0xF800) >> 11) << 3;
								u8G = ((pu16Temp[i] & 0x07E0) >> 5) << 2;
								u8B = (pu16Temp[i] & 0x001F) << 3;
								bmp_flr[i*4] = u8B;
								bmp_flr[i*4 + 1] = u8G;
								bmp_flr[i*4 + 2] = u8R;
								bmp_flr[i*4 + 3] = 0xFF;
							}		
							memcpy(bmp_flr_data,bmp_flr,4*1024*1024);	
					  	}
					  	else
					  	{
					  	for(i = 0;i<(int)(info_flr->height);i++)					  	
						memcpy(&bmp_flr_data[((int)(info_flr->height)-1-i)*info_flr->bpp/8*(int)(info_flr->width)],\
							info_flr->pixel_array+i*(int)(info_flr->width)*info_flr->bpp/8,(int)(info_flr->width)*info_flr->bpp/8);
							
						}
						
	    				ChangeNametoLow(fn);
	    				
					  	strcpy(File_RAM[savepath][file_num].name,fn);

						File_RAM[savepath][file_num].pic_info.img_base = pic_addr;
						
						File_RAM[savepath][file_num].pic_info.img_xlen = info_flr->width;
						File_RAM[savepath][file_num].pic_info.img_ylen = info_flr->height;
						File_RAM[savepath][file_num].pic_info.img_bpp = 32;
						v4a_fillvram(pic_addr,(int)(info_flr->width)*(int)(info_flr->height),(int *)bmp_flr_data);
						//if(file_num >1)
						//v4a_copyimage(&File_RAM[savepath][file_num-2].pic_info,&overlay_fbdisplay,gui_point(1024, 0));
						//usleep(300000);
						
						pic_addr +=  (File_RAM[savepath][file_num].pic_info.img_xlen*File_RAM[savepath][file_num].pic_info.img_ylen*4);
						
						}
						file_num++;

	    	    		 
	    			  }
				
	    		  }
	    		  fclose (pic_ptr);
	    	 }
	      }
		filenummax[savepath]=file_num-1;
		//printf("filenummax[savepath]=%d\n",filenummax[savepath]);
	      }
	}
	
	closedir(dir);
	return 0;
}

void CopyAllToRAM(void)
{
	printf("CopyAllToRAM start...\n");
	char ** folder_spi;
	unsigned int i;

	folder_spi = pDir_SpiFlash[G_DispMode];

	i= 0;

	while(1)
	{
		printf("CopyAllToRAM:%s\n",*folder_spi);
		CopyFolderToRAM(*folder_spi,i);

		folder_spi++;
		i++;
		if(i == 7) break;
	}


	
}

unsigned int RAM_f_open(unsigned int folder,char *name)
{
	filestruct * fileptr;
	unsigned char index;
	char *p_name;
	char *disname;

	disname = name;
	ChangeNametoLow(disname);
	if(folder > FOLDER_NUM)
	{
		return 1;
	}

	fileptr = File_RAM[folder];
	index = 0;
	while(index <= filenummax[folder])
	{
		p_name = fileptr->name;		
		if(!strcmp(p_name,disname))
		{
			memcpy(&ptr,&File_RAM[folder][index].pic_info,sizeof(File_RAM[folder][index].pic_info));
			return 0;
		}
		fileptr++;
		index++;
	}

	return 1;
}






